﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoGo
{

    class Game
    {
        private const int GameWidth = 800;
        private const int GameHeight = 700;
        const int Cols = 8;
        const int Rows = 7;
        private List<Block> Blocks { get; set; }
        public delegate void GameChanged();
        public event GameChanged gamechanged;

        public Game()
        {
            //初始化Block
            Blocks = new List<Block>();
            for (int i = 0; i < (Cols - 2); i++)
            {
                for (int j = 0; j < (Rows - 2); j++)
                {
                    Point point = new Point(100 * i + 100, 100 + 100 * j);

                    BlockStatus status = (BlockStatus)System.Enum.Parse(typeof(BlockStatus), (j + 1).ToString());
                    Blocks.Add(new Block(point, status));

                }
            }
            //生成一个中间变量的block用于交换
            Point point0 = new Point(8888, 8888);
            Blocks.Add(new Block(point0, BlockStatus.Pink));
           
            //打乱block
                Block blocknull = Blocks[30];
                Random randomi = new Random();
                for (int i = 0; i < 1000; i++)
            {
                int randi = randomi.Next(0, 30);
                bool Isrand = true;
                int randj = 0;
                do
                {
                    Random randomj = new Random();
                    for (int j = 0; j < 1000; j++)
                    { 
                        int rand3 = randomj.Next(0, 30);
                    if (rand3 != randi)
                    {
                        Isrand = false;
                        randj = rand3;
                    }
                    }
                } while (Isrand);
                blocknull.Status = Blocks[randi].Status;
                Blocks[randi].Status = Blocks[randj].Status;
                Blocks[randj].Status = blocknull.Status;

            }
        }
        public void Draw(Graphics g, Size size)
        {

            //定义空白的图
            Image img = new Bitmap(GameWidth, GameHeight);

            Graphics _g = Graphics.FromImage(img);
            //定义一个矩形，长宽是预定义
            Rectangle rec = new Rectangle(new Point(0, 0), new Size(GameWidth, GameHeight));
            _g.DrawImage(Properties.Resources.bg, rec);


            foreach (var block in Blocks)
            {
                block.Draw(_g);
            }
          

            //分格子
            int unitColWidth = size.Width / (Cols + 1);
            int unitRowHeight = size.Height / (Rows + 1);

            //画图
            g.DrawImage(img, new Rectangle(new Point(0, 0), size));

            }
        public void MouseDown(Point point, Size size)
        {
            //当前block的长方形
            Rectangle rectangle = new Rectangle(new Point(0, 0), size);
            //判断消除

         
        }
        public bool IsBlank()
        {
            return false;
        }
    }

        }
    
